package com.example.danangtravel;

import com.exam.danangtranel.Shopping.Tbshopping_activity;
import com.example.danangtravel.ATM.ATM_Activity;
import com.example.danangtravel.GioiThieu.IntroductionActivity;
import com.example.danangtravel.Hotel.TabHotelActivity;
import com.example.danangtravel.LienHe.LienheAcitivity;
import com.example.danangtravel.Restaurant.Tbfood_activity;
import com.example.danangtravel.TaxiATM.TaxiActivity;
import com.example.danangtravel.Travel.TravelActivity1;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Facebook.DialogListener;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class Home extends Activity {
	static Intent intent;
	public LocationManager locationManager;
	public MyLocationListener myLocationListener;
	public static double latitude;
	public static double longitude;
	public static checknetwork check;
	private Facebook mFacebook;
	private static final String[] PERMISSIONS = new String[] {
			"publish_stream", "read_stream", "offline_access" };
	private static final String APP_ID = "280195418722115";
	private SharedPreferences mPrefs;
	public String access_token;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		check = new checknetwork(this);
		if (check.isOnline()) {
			locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
			myLocationListener = new MyLocationListener(this);
			locationManager.requestLocationUpdates(
					LocationManager.NETWORK_PROVIDER, 10, 30,
					myLocationListener);
		}
		mFacebook = new Facebook(APP_ID);
		mPrefs = getPreferences(MODE_PRIVATE);
		access_token = mPrefs.getString("access_token", null);
		long expires = mPrefs.getLong("access_expires", 0);
		if (expires != 0) {
			mFacebook.setAccessExpires(expires);
		}

	}

	public void onClick(View view) {
		switch (view.getId()) {
		// Button khach san
		case R.id.khachsan:
			intent = new Intent(view.getContext(), TabHotelActivity.class);
			startActivityForResult(intent, 100);
			break;

		// Button du lich
		case R.id.dulich:
			// intent = new Intent(view.getContext(), TravelActivity.class);
			intent = new Intent(view.getContext(), TravelActivity1.class);
			startActivityForResult(intent, 100);
			break;

		// Button am thuc
		case R.id.amthuc:
			intent = new Intent(view.getContext(), Tbfood_activity.class);
			startActivityForResult(intent, 100);
			break;

		// Button mua sam
		case R.id.muasam:
			intent = new Intent(view.getContext(), Tbshopping_activity.class);
			startActivityForResult(intent, 100);
			break;

		// Button taxi
		case R.id.taxi:
			intent = new Intent(view.getContext(), TaxiActivity.class);
			startActivityForResult(intent, 100);
			break;
		// Button atm
		case R.id.atm:
			intent = new Intent(view.getContext(), ATM_Activity.class);
			startActivityForResult(intent, 100);
			break;

		// Button huong dan
		case R.id.huongdan:
			intent = new Intent(view.getContext(), huongdan_activity.class);
			startActivityForResult(intent, 100);
			break;

		// Button lien he
		case R.id.lienhe:
			intent = new Intent(view.getContext(), LienheAcitivity.class);
			startActivityForResult(intent, 100);
			break;

		// Button gioi thieu
		case R.id.gioithieu:
			intent = new Intent(view.getContext(), IntroductionActivity.class);
			startActivityForResult(intent, 100);
			break;
		case R.id.btn_facebook:
			check = new checknetwork(view.getContext());
			if (check.isOnline()) {
				if (access_token == null) {
					connectfacebook();
				} else
					postToWall();
			} else
				Toast.makeText(view.getContext(), "Không có kết nối !", 1000)
						.show();
			break;
		}
	}

	public class MyLocationListener implements LocationListener {

		protected Context mContext;

		public MyLocationListener(Context context) {
			this.mContext = context;
		}

		public void onLocationChanged(Location loc) {
			latitude = loc.getLatitude();
			longitude = loc.getLongitude();

		}

		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub

		}

		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub

		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub

		}
	}

	public void connectfacebook() {
		if (!mFacebook.isSessionValid()) {
			mFacebook.authorize(this, PERMISSIONS, new DialogListener() {

				public void onCancel() {
					// Function to handle cancel event
				}

				public void onComplete(Bundle values) {
					SharedPreferences.Editor editor = mPrefs.edit();
					editor.putString("access_token", mFacebook.getAccessToken());
					editor.putLong("access_expires",
							mFacebook.getAccessExpires());
					editor.commit();
				}

				public void onError(DialogError error) {
					// Function to handle error
				}

				public void onFacebookError(FacebookError fberror) {
					// Function to handle Facebook errors

				}

			});
		}
	}

	public void postToWall() {
		// post on user's wall.
		mFacebook.dialog(this, "feed", new DialogListener() {

			public void onFacebookError(FacebookError e) {
			}

			public void onError(DialogError e) {
			}

			public void onComplete(Bundle values) {
			}

			public void onCancel() {
			}
		});

	}
}
